LeetCode 19. Remove Nth Node From End of List
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
难度:Medium
题目描述:
Given a linked list, remove the n-th node from the end of list and return its head.
输入样例:
Example 1:
1 | Given linked list: 1->2->3->4->5, and n = 2. |
Note:
1 | Given n will always be valid. |
该题已提供代码:
1 | /** |
解题思路(快慢指针思想)
- 初始化一个
DummyNode
,让快慢指针都指向它,需要删除第n
个节点,就将该节点的前驱直接指向该节点的后继即可。
- 先将快指针移动到第
n
的位置上。
- 再将双指针同时移动。
实现代码
1 | class Solution { |
LeetCode测试结果
结语
至此,Remove Nth Node From End of List 解决完毕!